home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1504 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  59 lines

  1. Path: castle.nando.net!news
  2. From: actuary@nando.net   (Bill McCarthy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help with simple code
  5. Date: 14 Jan 1996 20:02:13 GMT
  6. Organization: News & Observer Public Access
  7. Message-ID: <4dbng5$e9o@castle.nando.net>
  8. References: <4dbak5$oij@ionews.io.org>
  9. Reply-To: actuary@nando.net (Bill McCarthy)
  10. NNTP-Posting-Host: grail801.nando.net
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4dbak5$oij@ionews.io.org>, jgordon@io.org (John Gordon MacPherson) writes:
  14. >Can anyone tell me what's wrong with this piece of code? I lifted it
  15. >straight from a textbook.
  16. >
  17. >Here's the code:
  18. >
  19. >/* Calculating compound interest */
  20. >#include <stdio.h>
  21. >#include <math.h>
  22. >
  23. >main()
  24. >{
  25. >    int year;
  26. >    double amount, principal = 1000, rate = 0.5;
  27. >
  28. >    printf("%4s%21s\n", "Year", "Amount on deposit");
  29. >
  30. >    for (year = 1; year <= 10; year++) {
  31. >        amount = principal * pow(1.0 + rate, year);
  32. >        printf("%4d%21.2f\n", year, amount);
  33. >    }
  34. >
  35. >    return 0;
  36. >}
  37. >______________________________________________________________
  38. >
  39. >and here's the error:
  40. >
  41. >In function `main':
  42. >undefined reference to `pow'
  43. >
  44. >I don't understand. `pow' is a function, not a variable?
  45. >Can anyone tell me what's wrong?
  46.  
  47. If that is a compiler error, you need a new compiler.  If it was
  48. a link error, you probably have not set up your environment
  49. properly.  Your book's code is OK but not efficient.  There is
  50. no need for using pow().
  51.  
  52. You could initialize <amount = principal;> and replace the first
  53. line of the 'for' loop with <amount *= 1 + rate;>
  54.  
  55. Bill McCarthy
  56. actuary@nando.net
  57. Wendell, NC  USA
  58.  
  59.